using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VGMSQ
    {
        public DataRow Convert2GMSQ(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryGMSQ(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["GMSMANDANT"] = " ".PadRight(2 ); 
         dwReturn["GMSNRKRS"] = " ".PadRight(2 ); 
         dwReturn["GMSLFDNR"] = " ".PadRight(4 ); 
         dwReturn["GMSPOS"] = " ".PadRight(2 ); 
         dwReturn["GMSSEQNR"] = " ".PadRight(0 ); 
         dwReturn["GMSAENZL"] = " ".PadRight(0 ); 
         dwReturn["GMSAENB"] = " ".PadRight(8 ); 
         dwReturn["GMSAEND"] = " ".PadRight(0 ); 
         dwReturn["GMSNEUB"] = " ".PadRight(8 ); 
         dwReturn["GMSNEUD"] = " ".PadRight(0 ); 
         dwReturn["GMSSTKNR"] = " ".PadRight(12 ); 
         dwReturn["GMSVERBPOS"] = " ".PadRight(0 ); 
         dwReturn["GMSPARTIE"] = " ".PadRight(7 ); 
         dwReturn["GMSPARTIEKZ"] = " ".PadRight(1 ); 
         dwReturn["GMSLO"] = " ".PadRight(2 ); 
         dwReturn["GMSLB"] = " ".PadRight(2 ); 
         dwReturn["GMSLP"] = " ".PadRight(8 ); 
         dwReturn["GMSLBEH"] = " ".PadRight(10 ); 
         dwReturn["GMSLBEHI"] = " ".PadRight(10 ); 
         dwReturn["GMSFILLER"] = " ".PadRight(26 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapGMSQ(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveGMSQ(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryGMSQ(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapGMSQ(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertGMSQ(dwLocal, ITXDBVERSION);
            }
            UpdateGMSQ(dwLocal);
        }
        public void InsertGMSQ(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO GMSQ(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryGMSQ(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM GMSQ WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM GMSQ  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "GMSQ";
            Return = Result;
            return Return;
        }
        public void DeleteGMSQ(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM GMSQ   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateGMSQ(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE GMSQ SET "
                    +"GMSMANDANT= :GMSMANDANT,"
                    +"GMSNRKRS= :GMSNRKRS,"
                    +"GMSLFDNR= :GMSLFDNR,"
                    +"GMSPOS= :GMSPOS,"
                    +"GMSSEQNR= :GMSSEQNR,"
                    +"GMSAENZL= :GMSAENZL,"
                    +"GMSAENB= :GMSAENB,"
                    +"GMSAEND= :GMSAEND,"
                    +"GMSNEUB= :GMSNEUB,"
                    +"GMSNEUD= :GMSNEUD,"
                    +"GMSSTKNR= :GMSSTKNR,"
                    +"GMSVERBPOS= :GMSVERBPOS,"
                    +"GMSPARTIE= :GMSPARTIE,"
                    +"GMSPARTIEKZ= :GMSPARTIEKZ,"
                    +"GMSLO= :GMSLO,"
                    +"GMSLB= :GMSLB,"
                    +"GMSLP= :GMSLP,"
                    +"GMSLBEH= :GMSLBEH,"
                    +"GMSLBEHI= :GMSLBEHI,"
                    +"GMSFILLER= :GMSFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("GMSMANDANT", OracleType.NVarChar).Value = dwData["GMSMANDANT"].ToString(); 
          or1.Parameters.Add("GMSNRKRS", OracleType.NVarChar).Value = dwData["GMSNRKRS"].ToString(); 
          or1.Parameters.Add("GMSLFDNR", OracleType.NVarChar).Value = dwData["GMSLFDNR"].ToString(); 
          or1.Parameters.Add("GMSPOS", OracleType.NVarChar).Value = dwData["GMSPOS"].ToString(); 
          or1.Parameters.Add("GMSSEQNR", OracleType.NVarChar).Value = dwData["GMSSEQNR"].ToString(); 
          or1.Parameters.Add("GMSAENZL", OracleType.NVarChar).Value = dwData["GMSAENZL"].ToString(); 
          or1.Parameters.Add("GMSAENB", OracleType.NVarChar).Value = dwData["GMSAENB"].ToString(); 
          or1.Parameters.Add("GMSAEND", OracleType.NVarChar).Value = dwData["GMSAEND"].ToString(); 
          or1.Parameters.Add("GMSNEUB", OracleType.NVarChar).Value = dwData["GMSNEUB"].ToString(); 
          or1.Parameters.Add("GMSNEUD", OracleType.NVarChar).Value = dwData["GMSNEUD"].ToString(); 
          or1.Parameters.Add("GMSSTKNR", OracleType.NVarChar).Value = dwData["GMSSTKNR"].ToString(); 
          or1.Parameters.Add("GMSVERBPOS", OracleType.NVarChar).Value = dwData["GMSVERBPOS"].ToString(); 
          or1.Parameters.Add("GMSPARTIE", OracleType.NVarChar).Value = dwData["GMSPARTIE"].ToString(); 
          or1.Parameters.Add("GMSPARTIEKZ", OracleType.NVarChar).Value = dwData["GMSPARTIEKZ"].ToString(); 
          or1.Parameters.Add("GMSLO", OracleType.NVarChar).Value = dwData["GMSLO"].ToString(); 
          or1.Parameters.Add("GMSLB", OracleType.NVarChar).Value = dwData["GMSLB"].ToString(); 
          or1.Parameters.Add("GMSLP", OracleType.NVarChar).Value = dwData["GMSLP"].ToString(); 
          or1.Parameters.Add("GMSLBEH", OracleType.NVarChar).Value = dwData["GMSLBEH"].ToString(); 
          or1.Parameters.Add("GMSLBEHI", OracleType.NVarChar).Value = dwData["GMSLBEHI"].ToString(); 
          or1.Parameters.Add("GMSFILLER", OracleType.NVarChar).Value = dwData["GMSFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
